<?php
require_once('gbif.checklist.php');
class Checklist11 extends Checklist{

	public function __construct( $file = NULL ) {
		parent::__construct($file);
	}

	public function validateChecklistData( $sheetIndex = 2 ) {
		if (!$this->fileOpen) $this->openFile();

		$passed = true;
		$wk = $this->objPHPExcel->setActiveSheetIndex( $sheetIndex );

		$highestRow = $wk->getHighestRow();
		$highestColumn = $wk->getHighestColumn();
		$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

		$rowIndex = -1;
		# Builds array but removes any blank rows
		foreach ($wk->getRowIterator() as $row) {
			$rowIndex++;
			$blankRow = true;

			$cellIterator = $row->getCellIterator();
			$cellIterator->setIterateOnlyExistingCells(false);
			$colIndex = -1;
			foreach ($cellIterator as $cell) {
				$colIndex++;
				$value = $cell->getValue();
//print $value . "<br>";
				# Check that data do not contain control characters that break the XML transfer format (e.g. vertical tabs).  Remove if included.
				$value = str_replace("\x0B", '', $value);  //remove all vertical tabs in sender string
#ADD MORE CONDITIONS
				$this->checklistArray[$rowIndex][$colIndex] = trim($value);
				if ($this->checklistArray[$rowIndex][$colIndex] != '') {
					$blankRow = false;
				}
			}

			if ($blankRow || $rowIndex == 0) {
				// Clean blank empty rows
				unset($this->checklistArray[$rowIndex]);
			}
		}
		return( $passed );
	}

	public function writechecklistData($path = '') {
		$fp = fopen($path . $this->checklistFilename, 'w');
		fputs($fp, "\xEF\xBB\xBF"); 
		fputcsv($fp, $this->checklistOutputHeaderList );

		$fpVernacular = fopen( $path . $this->vernacularFilename, 'w');
		fputs($fpVernacular, "\xEF\xBB\xBF"); 

		fputcsv($fpVernacular, $this->vernacularHeaderList);

		$fpDistribution = fopen( $path . $this->distributionFilename, 'w');
		fputs($fpDistribution, "\xEF\xBB\xBF"); 
		fputcsv($fpDistribution, $this->distributionHeaderList);

		if(is_array($this->checklistArray)) {
		foreach ($this->checklistArray as $row) {

			fputcsv($fp, array(
				  $row[0]
				, $row[1]
				, ''
				, $row[2]
				, $row[3]
				, $row[7]
				, $row[4]
				, ''
				, $row[13]
			));

			# Add Synonyms
			if (trim($row[5]) != '') {
				$this->writeSynonymData( $fp, $row[0], $row[5] );
			}

			# If common name exists send to vernacular file to be added
			if ($row[6] != '') {
				$this->writeVernacularData($fpVernacular, $row[0], $row[6]);
			}

			# If Distribution names exist send to file to be added
			if ((trim($row[8]) != '') 
				|| (trim($row[9]) != '')
				|| (trim($row[10]) != '')
				|| (trim($row[11]) != '')
			){
				$this->writeDistributionData($fpDistribution, $row[0], array('countries' => $row[8], 'delimiter' => ','), $row[9], $row[10], $row[11]);
			}
		} # foreach checklist
		}
			
		fclose($fp);
		fclose($fpVernacular);
	}
}
?>